﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Assembler_Compiler
{
    public class Function
    {
        private class Format
        {
            private class SizeValuesAndCodes
            {
                public Int16 W;
                public Int16 Size;
                
                public SizeValuesAndCodes(Int16 w, Int16 size)
                {
                    this.W = w;
                    this.Size = size;
                }                
            }

            private string name;
            private List<SizeValuesAndCodes> sizeValues;
            private string[] code;

            public Format(string formatName, string[] codeValue)
            {
                this.name = formatName;
                this.code = codeValue;
                this.sizeValues = new List<SizeValuesAndCodes>();
            }

            public string Name
            {
                get
                {
                    return this.name;
                }
            }

            public void AddSizeValue(Int16 wValue, Int16 size)
            {
                this.sizeValues.Add(new SizeValuesAndCodes(wValue, size));
            }

            public Int16 GetFormatSize(Int16 w)
            {
                return this.sizeValues.Find(c => c.W == w).Size;
            }

            public string[] Code
            {
                get
                {
                    return this.code;
                }
            }
        }

        private string name;

        private List<Format> formats;

        public string GetFuncName()
        {
            return (String)this.name.Clone();
        }

        public Function(string funcName)
        {
            this.name = funcName;
            this.formats = new List<Format>();
        }

        public void AddFormat(string formatNameValue, string[] codeValue)
        {
            this.formats.Add(new Format(formatNameValue, codeValue));
        }

        public void AddPairToFormat(string formatName, Int16 wValue, Int16 size)
        {
            Format temp = this.formats.Find(c => c.Name == formatName);
            temp.AddSizeValue(wValue, size);
        }

        public Int16 GetSize(string formatName, Int16 wValue)
        {
            Format test = this.formats.Find(c => c.Name == formatName);
            Int16 result = test.GetFormatSize(wValue);
            return result;
        }

        public string[] GetMachineCode(string formatName)
        {
            Format test = this.formats.Find(c => c.Name == formatName);
            return (string[])test.Code.Clone();
        }
    }
}